package com.weight.common.generate.temple;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.weight.common.generate.base.CommonUtil;
import com.weight.common.generate.base.FieldTypeEnum;
import com.weight.common.generate.base.JavaFile;
/**
* 
* 
*/
public class POJOClassFileMapper extends JavaFile {
	public POJOClassFileMapper(String filePath, String mainPackage, String curPackage, String modelName,List<String> fieldENList,List<String> fieldCNList,List<String> fieldTypeList,String tableCnName) {
		
		super(filePath,getFileName(curPackage,modelName)+".java", mainPackage+"."+curPackage, CLASS_TYPE, getFileName(curPackage,modelName));
		List<String> imports = new LinkedList<String>();
		List<String> classAnnotations = new LinkedList<String>();
		List<String> extendss = new LinkedList<String>();
		List<String> importPackagePaths = new LinkedList<String>();
		List<JavaFile.Field> fieldLists = new LinkedList<Field>();
		List<Method> methodLists = new LinkedList<Method>();
		//imports
		imports.add("java.io.Serializable");
		imports.add("cn.afterturn.easypoi.excel.annotation.Excel");
		imports.add("cn.afterturn.easypoi.excel.annotation.ExcelTarget");
//		imports.add("java.sql.Timestamp");
		imports.add("java.math.BigDecimal");
		imports.add("java.util.Date");
		imports.add("lombok.Data");
		if(curPackage.equals("VO")) {
			imports.add("com.alibaba.fastjson.annotation.JSONField");
//			imports.add("com.fasterxml.jackson.annotation.JsonProperty");
			imports.add("javax.validation.constraints.NotBlank");
			imports.add("javax.validation.constraints.NotNull");
		}
		classAnnotations.add("Data");
		
		importPackagePaths.add("Serializable");

		//属性
		List<String> fannotations = null;
		String fmodifier = "private static final";
		String ftype = "long";
		String fname = "serialVersionUID";
		String value = "2814639517974791520L";
		Field field = new Field(fannotations, fmodifier , ftype, fname, value);
		fieldLists.add(field);
		
		
		
		//normal、ral
		if(!modelName.endsWith("Info")&&!modelName.endsWith("Log")) {
			fmodifier = "private";
			ftype = "Integer";
			String strId=new StringBuilder().append(modelName).append("Id").toString();
			fannotations= new LinkedList<String>();
			 
			field = new Field(fannotations, fmodifier , ftype, strId, null,strId);
			fieldLists.add(field);
		}
		
		 
//属性		
		fmodifier = "private";
		ftype = "String";
		for(int i=0;i<fieldENList.size();i++) {
			fannotations= new LinkedList<String>();
			ftype=FieldTypeEnum.fromResultEnumPo(fieldTypeList.get(i).toUpperCase());
			if(curPackage.equals("VO")) {
				fannotations.add(new StringBuilder().append("Excel(name = \"").append(fieldCNList.get(i)).append("\")").toString());
//				@NotEmpty 用在集合类上面
//				@NotBlank 用在String上面
//				@NotNull    用在基本类型上
				if(ftype.equals("String")||ftype.equals("Date")) {
					ftype="String";
					if(i!=0)fannotations.add(new StringBuilder().append("NotBlank(message = \"").append(fieldCNList.get(i)).append("不能为空\")").toString());
				}else{
					fannotations.add(new StringBuilder().append("NotNull(message = \"").append(fieldCNList.get(i)).append("不能为空\")").toString());
				}
//				fannotations.add(new StringBuilder().append("JsonProperty(\"").append(fieldENList.get(i)).append("\")").toString());
				fannotations.add(new StringBuilder().append("JSONField(name = \"").append(fieldENList.get(i)).append("\")").toString());
			}
//			fannotations.add(new StringBuilder().append("Excel(name = \"").append(fieldCNList.get(i))
//					.append("\", orderNum = \"").append(i).append("\", mergeVertical = true, isImportField = \"")
//					.append(fieldENList.get(i)).append("\")").toString());
			field = new Field(fannotations, fmodifier , ftype, fieldENList.get(i), null,fieldCNList.get(i));
			fieldLists.add(field);
		}
		
		if((modelName.endsWith("Info")||modelName.endsWith("Log"))&&!curPackage.equals("VO")) {
			String[] fieldCn = new String[]{"是否删除","创建时间","修改时间","创建人","修改人","备用字段1","备用字段2","备用字段3"};
			String[] fieldEn=new String[]{"isDel","createTime","updateTime","createBy","updateBy","standbyfield1","standbyfield2","standbyfield3"};
			String[] fieldType=new String[]{"TINYINT","TIMESTAMP","TIMESTAMP","VARCHAR","VARCHAR","VARCHAR","VARCHAR","VARCHAR"};
			for(int i=0;i<fieldCn.length;i++) {
				fannotations= new LinkedList<String>();
				ftype=FieldTypeEnum.fromResultEnumPo(fieldType[i].toUpperCase());
				field = new Field(fannotations, fmodifier , ftype, fieldEn[i], null,fieldCn[i]);
				fieldLists.add(field);
			}
		}
		
//无参构造器
		List<String> mannotations = new LinkedList<String>();
		String mmodifier = "public";
		String resultType= "";
		String name= null;
		String args= null;
		List<String> exceptions = null;
		String methodBody = buildMethod(new String[] {"super();"});
		name=getFileName(curPackage,modelName);
		Method method = new Method(mannotations, mmodifier, resultType, name, args, exceptions, methodBody);
		methodLists.add(method);
		 
//有参构造器
		StringBuilder tt=new StringBuilder();
		String[] ss=new String[fieldENList.size()+1];
		ss[0]="super();";
		for(int i=0;i<fieldENList.size();i++) {
			if(i>0)tt.append(",");
			
			ftype=FieldTypeEnum.fromResultEnumPo(fieldTypeList.get(i).toUpperCase());
			if(curPackage.equals("VO")&&ftype.equals("Date")) {
				ftype="String";
			}
			tt.append(ftype).append(" ").append(fieldENList.get(i));
			
			ss[i+1]=new StringBuilder().append("this.").append(fieldENList.get(i)).append(" = ").append(fieldENList.get(i)).append(";").toString();
		}
		args=tt.toString();
		methodBody= buildMethod(ss);
		method = new Method(mannotations, mmodifier, resultType, name, args, exceptions, methodBody);
		methodLists.add(method);
//get/set方法		
//		mmodifier = "public";
//		for(int i=0;i<fieldENList.size();i++) {
//			//getId方法
//			name = new StringBuilder().append("get").append(getModelClass(fieldENList.get(i))).toString();
//			if("createTime,updateTime".contains(fieldENList.get(i))) {
//				resultType = "Timestamp";
//			}else {
//				resultType = "String";
//			}
//			args = null;
//			methodBody = buildMethod(new String[] {
//				new StringBuilder().append("return ").append(fieldENList.get(i)).append(";").toString()
//			});
//			method = new Method(mannotations, mmodifier, resultType, name, args, exceptions, methodBody);
//			methodLists.add(method);
//			//setId方法
//			mannotations = null;
//			resultType = "void";
//			name = new StringBuilder().append("set").append(getModelClass(fieldENList.get(i))).toString();
//			if("createTime,updateTime".contains(fieldENList.get(i))) {
//				args = new StringBuilder().append("Timestamp ").append(fieldENList.get(i)).toString();
//			}else {
//				args = new StringBuilder().append("String ").append(fieldENList.get(i)).toString();
//			}
//			methodBody = buildMethod(new String[] {
//					new StringBuilder().append("this.").append(fieldENList.get(i)).append(" = ").append(fieldENList.get(i)).append(";").toString()
//			});
//			method = new Method(mannotations, mmodifier, resultType, name, args, exceptions, methodBody);
//			methodLists.add(method);
//		}
		/**	类注释	*/
		Collection<String> classDescriptionList=CommonUtil.descriptionModel(new StringBuilder(tableCnName), null, false);
		super.init(imports, classAnnotations, extendss, importPackagePaths, fieldLists, methodLists,classDescriptionList);
	}
	private static String buildMethod(String[] lines) {
		StringBuilder method = new StringBuilder();
		if(lines != null) {
			for (String line : lines) {
				method
				.append("\t\t")
				.append(line)
				.append("\n");
			}
		}
		return method.toString();
	}
	public static String getFileName(String curPackage, String modelName) {
		if(curPackage.equals("VO")) return getModelClass(modelName).append("VO").toString();
		return getModelClass(modelName).toString();
	} 
}
